package com.ludaxu.google.protocol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.IOUtils;
import com.ludaxu.google.domain.AppInfo;
import com.ludaxu.google.tools.FileUtils;
import com.ludaxu.google.tools.UiUtils;

public abstract class BaseProtocol<T> {
	private String json = null;
	private static String result = null;

	public T load(int index) {
		json = loadLocal(index);
		if (json == null) {
			// 请求服务器
			json = loadServer(index);
			System.out.println("加载服务器数据完成." + json);
			if (json != null) {
				saveLocal(json, index);
			}
		} else {
			System.out.println("复用了本地缓存");
		}
		if (json != null) {
			return paserJson(json);
		} else {
			return null;
		}

	}

	private void saveLocal(String json, int index) {

		// 在第一行写一个过期的时间
		File dir = FileUtils.getCacheDir();
		BufferedWriter bw = null;
		try {
			File file = new File(dir, getKey() + "_" + index);
			FileWriter fw = new FileWriter(file);
			bw = new BufferedWriter(fw);
			bw.write(System.currentTimeMillis() + 1000 * 100 + "");
			bw.newLine();// 换行
			bw.write(json);
			bw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(bw);
			System.out.println("文件写入成功");

		}

	}

	private String loadLocal(int index) {
		// 如果发现文件过期 就不要再去复用缓存了
		File dir = FileUtils.getCacheDir();// 获取缓存所在的文件夹
		File file = new File(dir, getKey() + "_" + index);
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			long outOfDate = Long.parseLong(br.readLine());// 读一行
			if (System.currentTimeMillis() > outOfDate) {
				return null;
			} else {
				String str = null;
				StringWriter sw = new StringWriter();
				// 一行一行的读到内存中
				while ((str = br.readLine()) != null) {
					sw.write(str);
				}
				// 读取到json的结果
				return sw.toString();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private String loadServer(int index) {
		// xUtils afinal
		HttpUtils httpUtils = new HttpUtils();
		String url = "http://127.0.0.1:8090/" + getKey() + "?index=" + index;
		System.out.println("路径:" + url);
		httpUtils.send(HttpMethod.GET, "http://127.0.0.1:8090/" + getKey()
				+ "?index=" + index, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				Toast.makeText(UiUtils.getContext(), "联网失败", 0).show();
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				result = arg0.result;
				Toast.makeText(UiUtils.getContext(), "联网成功", 0).show();
				System.out.println("联网成功的结果:" + result);
			}

		});
		System.out.println("从服务器获取数据结果:" + result);
		return result;
	}

	/**
	 * 说明了关键字
	 * 
	 * @return
	 */
	public abstract String getKey();

	/**
	 * 解析json
	 * 
	 * @param json
	 * @return
	 */
	public abstract T paserJson(String json);

}
